package com.zy.mc_module.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.zy.mc_module.base.App;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created on 19/1/14
 */
public class FileUtils {

    /**
     * 根据文件路径读取文本
     *
     * @param StrFile
     * @return
     */
    public static String readTxtFile(String StrFile) {
        String res = "";
        try {
            // 读取raw文件夹中的txt文件,将它放入输入流中
            // InputStream in = getResources().openRawResource(R.raw.ansi);
            // 读取assets文件夹中的txt文件,将它放入输入流中
            InputStream in = App.getAppContext().getResources().getAssets().open(StrFile);
            // 获得输入流的长度
            int length = in.available();
            // 创建字节输入
            byte[] buffer = new byte[length];
            // 放入字节输入中
            in.read(buffer);
            // 获得编码格式
            res = new String(buffer, "UTF-8");
            // 关闭输入流
            in.close();
        } catch (Exception e) {
            Logs.Log_e("FileUtils", "错误" + e.getMessage());
        }
        return res;
    }

    public static String creatFileName(Context context) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return context.getExternalCacheDir() + "/" + dateFormat.format(date) + ".mp4";
    }

    /**
     * 复制文件
     *
     * @param source 输入文件
     * @param target 输出文件
     */
    public static void copy(File source, File target) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(target);
            byte[] buffer = new byte[1024];
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath())));
    }
}

